##### CPU variants #####
GCCARCH = $(shell uname -m)

ifeq ($(GCCARCH),x86_64)
  GCCARCH = core2
endif

ifeq ($(findstring arm,$(GCCARCH)),arm)
  GCCARCH = armv6k
endif

version = $(shell grep RTPSENSOR_VERSION voipmonitor.h | cut -d" " -f 3 | tr -d "\"")

objects = codec_alaw.o codec_ulaw.o format_slinear.o format_wav.o format_ogg.o calltable.o rtp.o voipmonitor.o sniff.o jitterbuffer/astmm.o jitterbuffer/utils.o jitterbuffer/fixedjitterbuf.o jitterbuffer/jitterbuf.o jitterbuffer/abstract_jb.o jitterbuffer/frame.o gzstream/gzstream.o gzstream/libgzstream.a manager.o tools.o filter_mysql.o hash.o mos_g729.o odbc.o rtcp.o sql_db.o md5.o mirrorip.o ipaccount.o skinny.o

headers = $(shell ls *.h -1 | tr "\n" " ")

#args = -g3 -Wall  -march=$(GCCARCH)
#args = -O2 -Wall -march=$(GCCARCH)
#CFLAGS+=-I /usr/local/include/mysql++/ -I /usr/include/mysql++/ -I /usr/include/mysql/ -g3 -Wall -I jitterbuffer/  -L/usr/local/lib/ -Lgzstream/ -Lliblfds.6/bin/ -fPIC

#LDFLAGS := $(LDFLAGS) -static -static-libgcc

shared: LIBS=-lpthread -lpcap -lgzstream -lz -lvorbis -lvorbisenc -logg -lodbc -lmysqlclient -lrt # -llfds #-lcurl #-lnids
shared: CFLAGS+= -DQUEUE_NONBLOCK2 -I /usr/include/mysql/ -Wall -I jitterbuffer/  -L/usr/lib/mysql -L/usr/local/lib/ -Lgzstream/ -Lliblfds.6/bin/ -L/usr/lib64/mysql -fPIC -g3
shared: args = -g3 -Wall  -march=$(GCCARCH) 

static: LIBS=-static -L/usr/lib/mysql -L/usr/lib64/mysql -L/usr/lib/mysql -lodbc -lltdl -ldl -lrt -lz -lcrypt -lm -lssl -static-libstdc++ -static-libgcc -lpcap -L/usr/lib/mysql -lmysqlclient -lpthread -lgzstream -lz -lc -lvorbis -lvorbisenc -logg -lrt # -llfds 
static: CFLAGS+= -DQUEUE_NONBLOCK2 -I /usr/include/mysql/ -Wall -I jitterbuffer/  -L/usr/local/lib/ -Lgzstream/ -Lliblfds.6/bin/ -fPIC -O2
static: args = -O2 -Wall  -march=$(GCCARCH)

armv5shared: LIBS=-lpthread -lpcap -lgzstream -lz -lvorbis -lvorbisenc -logg -lodbc -lmysqlclient -lrt
armv5shared: CFLAGS+= -DQUEUE_NONBLOCK2 -DPACKED -I /usr/include/mysql/ -g3 -Wall -I jitterbuffer/  -L/usr/local/lib/ -Lgzstream/ -L/usr/lib64/mysql -fPIC
armv5shared: args = -g3 -Wall  

armv5static: LIBS=-static -L/usr/lib/mysql -lodbc -lltdl -ldl -lrt -lz -lcrypt -lm -lssl -static-libstdc++ -static-libgcc -lpcap -L/usr/lib/mysql -L/usr/lib64/mysql/ -lmysqlclient -lpthread -lgzstream -lz -lc -lvorbis -lvorbisenc -logg -lrt
armv5static: CFLAGS+= -DQUEUE_NONBLOCK2 -DPACKED -I /usr/include/mysql/ -g3 -Wall -I jitterbuffer/  -L/usr/local/lib/ -Lgzstream/ -L/usr/lib64/mysql -fPIC
armv5static: args = -g3 -Wall 

shared: cleantest $(objects) 
#	make -C liblfds.6
	g++ $(objects) ${CFLAGS} -o voipmonitor ${LIBS}

static: cleantest $(objects)
#	make -C liblfds.6
	g++ $(objects) ${CFLAGS} -o voipmonitor ${LIBS}

armv5shared: cleantest $(objects)
	g++ $(objects) ${CFLAGS} -o voipmonitor ${LIBS}

armv5static: cleantest $(objects)
	g++ $(objects) ${CFLAGS} -o voipmonitor ${LIBS}

cleantest:
	@cmp -s .cleancount .lastclean || $(MAKE) clean

gzstream/gzstream.o : gzstream/gzstream.C gzstream/gzstream.h
	g++ -I. -Igzstream/ -c -o gzstream/gzstream.o gzstream/gzstream.C

gzstream/libgzstream.a : gzstream/gzstream.o
	ar cr gzstream/libgzstream.a gzstream/gzstream.o

codec_alaw.o : codec_alaw.cpp codec_alaw.h ${headers}
	g++ -c codec_alaw.cpp $(args) ${CFLAGS}

codec_ulaw.o : codec_ulaw.cpp codec_ulaw.h ${headers}
	g++ -c codec_ulaw.cpp $(args) ${CFLAGS}

format_slinear.o : format_slinear.cpp format_slinear.h ${headers}
	g++ -c format_slinear.cpp $(args) ${CFLAGS}

format_wav.o : format_wav.cpp format_wav.h ${headers}
	g++ -c format_wav.cpp $(args) ${CFLAGS}

format_ogg.o : format_ogg.cpp format_ogg.h ${headers}
	g++ -c format_ogg.cpp $(args) ${CFLAGS}

calltable.o : calltable.cpp calltable.h ${headers}
	g++ -c calltable.cpp $(args) ${CFLAGS}

rtp.o : rtp.cpp rtp.h ${headers}
	g++ -c rtp.cpp $(args) ${CFLAGS}

rtcp.o : rtcp.cpp rtcp.h ${headers}
	g++ -c rtcp.cpp $(args) ${CFLAGS}

voipmonitor.o : voipmonitor.cpp voipmonitor.h ${headers}
	g++ -c voipmonitor.cpp $(args) ${CFLAGS}

sniff.o : sniff.cpp sniff.h ${headers}
	g++ -c sniff.cpp $(args) ${CFLAGS}

manager.o : manager.cpp manager.h ${headers}
	g++ -c manager.cpp $(args) ${CFLAGS}

tools.o : tools.cpp tools.h ${headers}
	g++ -c tools.cpp $(args) ${CFLAGS}

filter_mysql.o : filter_mysql.cpp filter_mysql.h ${headers}
	g++ -c filter_mysql.cpp $(args) ${CFLAGS}

hash.o : hash.cpp hash.h ${headers}
	g++ -c hash.cpp $(args) ${CFLAGS}

mos_g729.o : mos_g729.cpp mos_g729.h ${headers}
	g++ -c mos_g729.cpp $(args) ${CFLAGS}

sql_db.o : sql_db.cpp sql_db.h ${headers}
	g++ -c sql_db.cpp $(args) ${CFLAGS}

md5.o : md5.cpp md5.h ${headers}
	g++ -c md5.cpp $(args) ${CFLAGS}

mirrorip.o : mirrorip.cpp mirrorip.h ${headers}
	g++ -c mirrorip.cpp $(args) ${CFLAGS}

ipaccount.o : ipaccount.cpp ipaccount.h ${headers}
	g++ -c ipaccount.cpp $(args) ${CFLAGS}

skinny.o : skinny.cpp skinny.h ${headers}
	g++ -c skinny.cpp $(args) ${CFLAGS}

clean :
	make -C liblfds.6 clean
	rm -f $(objects) voipmonitor gzstream/*.o libgzstream.a
	cp -f .cleancount .lastclean

targz64: static
	rm -rf /tmp/voipmonitor-amd64-${version}-static*
	mkdir -p /tmp/voipmonitor-amd64-${version}-static/usr/local/sbin
	cp cdrtable.sql /tmp/voipmonitor-amd64-${version}-static/
	strip voipmonitor
	cp voipmonitor /tmp/voipmonitor-amd64-${version}-static/usr/local/sbin/
	chmod +x /tmp/voipmonitor-amd64-${version}-static/usr/local/sbin/voipmonitor
	mkdir -p /tmp/voipmonitor-amd64-${version}-static/etc/init.d
	cp config/voipmonitor.conf /tmp/voipmonitor-amd64-${version}-static/etc/
	cp config/init.d/voipmonitor /tmp/voipmonitor-amd64-${version}-static/etc/init.d/
	chmod +x /tmp/voipmonitor-amd64-${version}-static/etc/init.d/voipmonitor 
	cp scripts/install-script.sh /tmp/voipmonitor-amd64-${version}-static/
	chmod +x /tmp/voipmonitor-amd64-${version}-static/install-script.sh
	tar -C /tmp -czf /tmp/voipmonitor-amd64-${version}-static.tar.gz voipmonitor-amd64-${version}-static
	
targz32: static
	rm -rf /tmp/voipmonitor-i686-${version}-static*
	mkdir -p /tmp/voipmonitor-i686-${version}-static/usr/local/sbin
	cp cdrtable.sql /tmp/voipmonitor-i686-${version}-static/
	strip voipmonitor
	cp voipmonitor /tmp/voipmonitor-i686-${version}-static/usr/local/sbin/
	chmod +x /tmp/voipmonitor-i686-${version}-static/usr/local/sbin/voipmonitor
	mkdir -p /tmp/voipmonitor-i686-${version}-static/etc/init.d
	cp config/voipmonitor.conf /tmp/voipmonitor-i686-${version}-static/etc/
	cp config/init.d/voipmonitor /tmp/voipmonitor-i686-${version}-static/etc/init.d/
	chmod +x /tmp/voipmonitor-i686-${version}-static/etc/init.d/voipmonitor 
	cp scripts/install-script.sh /tmp/voipmonitor-i686-${version}-static/
	chmod +x /tmp/voipmonitor-i686-${version}-static/install-script.sh
	tar -C /tmp -czf /tmp/voipmonitor-i686-${version}-static.tar.gz voipmonitor-i686-${version}-static

source: clean
	rm -rf /tmp/voipmonitor-${version}-src
	mkdir /tmp/voipmonitor-${version}-src
	cp -a * /tmp/voipmonitor-${version}-src/
	rm -rf `find /tmp/voipmonitor-${version}-src -type d -name .svn`
	rm /tmp/voipmonitor-${version}-src/Makefile
	echo 1 > /tmp/voipmonitor-${version}-src/.cleancount
	tar -C /tmp -czf /tmp/voipmonitor-${version}-src.tar.gz voipmonitor-${version}-src


install: 
	install voipmonitor /usr/local/sbin/

